package com.linkage.educloud.ah.adapter.topic;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.view.activity.topic.TopicChildDetailActivity;
import com.linkage.educloud.ah.widget.ExpressionUtils;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/11/7.
 */

public class TopicChildListAdapter extends XRecyclerView.Adapter<TopicChildListAdapter.ViewHolder> {
    private List<TopicChildBean> mlist;
    private Context mcontext;

    public TopicChildListAdapter(Context context, List<TopicChildBean> datas) {
        this.mcontext = context;
        this.mlist = datas;
    }

    public void addAll(List<TopicChildBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_topic_child_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final TopicChildBean childBean = mlist.get(position);
        if (childBean.getStick().equals("1")) {
            viewHolder.item_topic_child_stick.setVisibility(View.VISIBLE);
        } else {
            viewHolder.item_topic_child_stick.setVisibility(View.GONE);
        }
        viewHolder.item_topic_child_name.setText(childBean.getName());
        viewHolder.item_topic_child_content.setText(ExpressionUtils.replaceFace(mcontext, childBean.getContent()));
        viewHolder.item_topic_child_nickname.setText(childBean.getNick());
        viewHolder.item_topic_child_time.setText(childBean.getPublish_time());
        viewHolder.item_topic_child_comment_num.setText(childBean.getComment_num() + "");

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicChildDetailActivity.launch(mcontext, childBean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        Button item_topic_child_stick;
        TextView item_topic_child_name;
        TextView item_topic_child_content;
        TextView item_topic_child_nickname;
        TextView item_topic_child_time;
        TextView item_topic_child_comment_num;

        public ViewHolder(View convertView) {
            super(convertView);
            item_topic_child_stick = convertView.findViewById(R.id.item_topic_child_stick);
            item_topic_child_name = convertView.findViewById(R.id.item_topic_child_name);
            item_topic_child_content = convertView.findViewById(R.id.item_topic_child_content);
            item_topic_child_nickname = convertView.findViewById(R.id.item_topic_child_nickname);
            item_topic_child_time = convertView.findViewById(R.id.item_topic_child_time);
            item_topic_child_comment_num = convertView.findViewById(R.id.item_topic_child_comment_num);
        }
    }
}
